package cn.sciento.farm.devicemanager.common.util.sms;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class AliyunSmsUtil {

    private final AliyunSmsProperties aliyunSmsProperties;

    private AliyunSmsUtil(AliyunSmsProperties aliyunSmsProperties){
        this.aliyunSmsProperties = aliyunSmsProperties;
    }

    public String sendSms(String phone , Map<String,String> params){
        DefaultProfile profile = DefaultProfile.getProfile(aliyunSmsProperties.getRegionId(), aliyunSmsProperties.getAccessKey(), aliyunSmsProperties.getAccessSecret());
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(aliyunSmsProperties.getSysDomain());
        request.setSysVersion(aliyunSmsProperties.getSysVersion());
        request.setSysAction(aliyunSmsProperties.getSysAction());
        request.putQueryParameter("PhoneNumbers",phone);
        request.putQueryParameter("SignName",aliyunSmsProperties.getSignName());
        request.putQueryParameter("TemplateCode",aliyunSmsProperties.getTemplateCode());
        request.putQueryParameter("RegionId", aliyunSmsProperties.getRegionId());
        request.putQueryParameter("SmsUpExtendCode",aliyunSmsProperties.getSmsUpExtendCode());
        request.putQueryParameter("TemplateParam", JSON.toJSONString(params));
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return  response.getData();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static final String ACCESS_KEY = "LTAI4FxyeapXUERQFrVVrkdY";
    private static  final String ACCESS_SECRET = "HdLzzjSGxLpE7uGcZXKiuux8CpaI6B";
    private static DefaultProfile profile = null;
    static {
         profile = DefaultProfile.getProfile("cn-hangzhou", ACCESS_KEY, ACCESS_SECRET);
    }

    public static void main(String[] args) {
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("PhoneNumbers","15626475084");
        request.putQueryParameter("SignName","赛通科技");
        request.putQueryParameter("TemplateCode","SMS_177540035");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("TemplateParam","{\"collectionName\":\"1222\",\"content\":\"错误\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

    public String sendTenantSms(String phone){
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", aliyunSmsProperties.getAccessKey(), aliyunSmsProperties.getAccessSecret());
        IAcsClient client = new DefaultAcsClient(profile);
        Map<String, String> params = new HashMap<>();
        params.put("collectionName", "余额");
        params.put("content", "剩余可发送短信为0条");
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysVersion(aliyunSmsProperties.getSysVersion());
        request.setSysDomain(aliyunSmsProperties.getSysDomain());
        request.setSysAction(aliyunSmsProperties.getSysAction());
        request.putQueryParameter("SignName",aliyunSmsProperties.getSignName());
        request.putQueryParameter("PhoneNumbers",phone);
        request.putQueryParameter("TemplateCode",aliyunSmsProperties.getTemplateCode());
        request.putQueryParameter("RegionId", aliyunSmsProperties.getRegionId());
        request.putQueryParameter("SmsUpExtendCode",aliyunSmsProperties.getSmsUpExtendCode());
        request.putQueryParameter("TemplateParam", JSON.toJSONString(params));
        try {
            CommonResponse response = client.getCommonResponse(request);
            return response.getData();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }
}
